import os, inspect 
from types import StringType, TupleType, ListType

f = open(os.getcwd() + '/doc/html_man/Ounk_man.tex', 'w')

# Header
f.write("\documentclass[12pt,oneside]{article}\n")
f.write("\usepackage[utf8]{inputenc}\n")
f.write("\usepackage{dcolumn,amsmath}\n\n")
f.write("\\begin{document}\n\n")

f.write("""The Ounk library contains two main kind of functions. The first one contains functions to write part of the csound file. This will defines the process chain to be compute by Csound. Another kind of functions are straigth Python functions. These functions serve to construct algorithmic musical patterns, manage Human Interface Device or build Graphical User Interface.\n\n""")

pyfunc = ['Algorithmic', 'GUI', 'Patterns']
csfunc = [key for key in sorted(LIB_INFO.keys()) if key not in pyfunc]

for keylist in [csfunc, pyfunc]:
    if keylist == csfunc:
        f.write('\chapter{Functions to write Csound file}\n\n')
    else:
        f.write('\chapter{Framework to write Python algorithms}\n\n')
    for key in keylist:
        f.write('\section{' + key + '}\n\n')
        f.write(LIB_DOC[key] + '\n\n')
        for func in sorted(LIB_INFO[key].keys()):
            try:
                br = LIB_INFO[key][func].__doc__.index('\n')
            except:
                br = len(LIB_INFO[key][func].__doc__)
            fstr = LIB_INFO[key][func].__doc__[:br]
            f.write('\subsection{' + func + '}\n\n')
            f.write(fstr + '\n\n')
            f.write('\\bigskip\n')
            f.write("{\\bf SYNTAX}\n\n")
            arglist = inspect.getargspec(LIB_INFO[key][func])
            if arglist[0] == None: l1 = 0
            else: l1 = len(arglist[0])
            if arglist[3] == None: l2 = 0
            else: l2 = len(arglist[3])
            skip = l1 - l2
            f.write(func + '(')
            for i in range(l1):
                f.write(arglist[0][i])
                if i >= skip:
                    arg = arglist[3][i-skip]
                    if arg == None:
                        default = 'None'
                    elif type(arg) == StringType:
                        default = "'%s'" % arg
                    elif type(arg) == TupleType:
                        default = '(' + ','.join('%s' % i for i in arg) + ')'
                    else:
                        default = '%s' % arglist[3][i-skip]
                    f.write('=' + default)
                if i < (l1-1):
                    f.write(', ')
            f.write(')\n\n')
            f.write('\\bigskip\n')
            lstr = LIB_INFO[key][func].__doc__.split('--- PARAMETERS ---')
            try:
                secondStr = lstr[1]
                f.write('{\\bf PARAMETERS}\n\n')
                for s in secondStr.split('\n'):
                    if '--- METHODS ---' in s:
                        f.write('{\\bf METHODS}\n\n')
                    else:
                        f.write(s + '\n\n')
            except:
                pass
            expath = os.getcwd() + '/doc/html_man/html_examples/' + func + '.py'
            if os.path.isfile(expath):
                f.write('\\bigskip\n')
                f.write('{\\bf EXAMPLE}\n')
                f.write('\\begin{verbatim}\n')
                ex = open(expath, 'r')
                for lin in ex.readlines():
                    f.write(lin)
                f.write('\end{verbatim}\n\n')
        f.write('\n\n')

f.write('\end{document}\n')
f.close()

os.chdir('doc/html_man')
os.system('latex2html -html_version 4.0,unicode -noinfo -long_titles 1 -noaddress Ounk_man.tex')
os.system('rm Ounk_man/WARNINGS')
os.chdir('../..')
